#include<bits/stdc++.h>
using namespace std;
int t, n, a[200001], pre[1000001];
long long ans, sum[200001], f[200001];
int main()
{
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	scanf("%d",&t);
	while(t--)
	{
		memset(pre,0,sizeof pre);
		memset(f,0,sizeof f);
		memset(sum,0,sizeof sum);
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			sum[i]=sum[i-1];
			if(a[i]==a[i-1])
				sum[i]+=a[i];
		}
		for(int i=1;i<=n;i++)
		{
			f[i]=f[i-1];
			if(!pre[a[i]])
			{
				pre[a[i]]=i;
				continue;
			}
			ans=sum[i-1]-sum[pre[a[i]]];
			ans+=f[pre[a[i]]+1];
			ans+=a[i];
			f[i]=max(f[i],ans);
			pre[a[i]]=i;
		}
		printf("%lld\n",f[n]);
	}
	return 0;
}